// /////////////////////////////////////////////////////////////////////////////
//
// Name:            ImageComponent.h
// Author:          Michael Bartsch (ZCCdark203)
//
// Desc :           Declaration of the ImageComponent class.
//
// License:         Copyright (C) 2009 Michael Bartsch and Contributors
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the zlib/libpng License.
//                  See main.cpp for conditions of distribution and use.
//
// /////////////////////////////////////////////////////////////////////////////

#ifndef __IMAGECOMPONENT_H__
#define __IMAGECOMPONENT_H__

// Include files
#include "../../dependencies.h"
#include "../../core/EntityComponent.h"


// ImageComponent class
//! Component wrapper for drawing 2d images.
//!
//! <b>XML:</b>
//! \code
//! <ImageComponent>
//!    <!-- Properties -->
//! </ImageComponent>
//! \endcode
class ImageComponent : public EntityComponent
{
public:

    // Initialisation and deinitialisation
    //! Constructor (default)
    //! @param parent        The parent entity to which the component should be added.
    ImageComponent(Entity *parent);
    //! Constructor
    //! @param parent        The parent entity to which the component should be added.
    //! @param fileName      Filename of the texture to load.
    //! @param position      Upper left 2d destination position where the image will be drawn.
    //! @param sourceRect    Source rectangle in the image.
    //! @param clipRect      Pointer to rectangle on the screen where the image is clipped to.
    //!                      If this pointer is NULL the image is not clipped.
    //! @param color         Color with which the image is drawn.
    //! @param alphaColor    Alpha color of the image.
    //! @param useAlphaColor If true, the alpha channel of the texture is used to draw the image.
    ImageComponent(Entity *parent, const std::string &fileName, const vector2di &position,
                   const rect<s32> &sourceRect, const rect<s32> &clipRect,
                   const SColor &color = SColor(255, 255, 255, 255),
                   const SColor &alphaColor = SColor(255, 255, 255, 255),
                   bool useAlphaColor = false);
    //! Constructor
    //! @param parent        The parent entity to which the component should be added.
    //! @param texture       Texture to be added to the image component.
    //! @param position      Upper left 2d destination position where the image will be drawn.
    //! @param sourceRect    Source rectangle in the image.
    //! @param clipRect      Pointer to rectangle on the screen where the image is clipped to.
    //!                      If this pointer is NULL the image is not clipped.
    //! @param color         Color with which the image is drawn.
    //! @param alphaColor    Alpha color of the image.
    //! @param useAlphaColor If true, the alpha channel of the texture is used to draw the image.
    ImageComponent(Entity *parent, ITexture *texture, const vector2di &position,
                   const rect<s32> &sourceRect, const rect<s32> &clipRect,
                   const SColor &color = SColor(255, 255, 255, 255),
                   const SColor &alphaColor = SColor(255, 255, 255, 255),
                   bool useAlphaColor = false);
    //! Deconstructor
    ~ImageComponent();

    // Public methods
    //! Gets the alpha color.
    const SColor& getAlphaColor() const;
    //! Gets the clipping rectangle.
    const rect<s32>& getClipRect() const;
    //! Gets the color with which the image is drawn.
    const SColor& getColor() const;
    //! Gets the upper left 2d screen position where the image will be drawn.
    const vector2di& getPosition() const;
    //! Gets the source rectangle in the image
    const rect<s32>& getSourceRect() const;
    //! Gets the texture associated with this component.
    const ITexture* getTexture() const;
    //! Returns whether the alpha color is used.
    bool getUseAlphaColor() const;

    //! Sets the alpha color of the image.
    //!
    //! <b>XML:</b>
    //! \code
    //! <alphaColor type="color" value="" />
    //! \endcode
    //!
    //! @param color         New alpha color of the image.
    void setAlphaColor(const SColor &color);
    //! Sets where the image is clipped to.
    //!
    //! <b>XML:</b>
    //! \code
    //! <clipRect type="rect" value="" />
    //! \endcode
    //!
    //! @param rectangle     Pointer to rectangle on the screen where the image is clipped to.
    //!                      If this pointer is NULL the image is not clipped.
    void setClipRect(const rect<s32> &rectangle);
    //! Sets the color with which the image is drawn.
    //!
    //! <b>XML:</b>
    //! \code
    //! <color type="color" value="" />
    //! \endcode
    //!
    //! @param color         New color.
    void setColor(const SColor &color);
    //! Sets the upper left 2d screen position where the image will be drawn.
    //!
    //! <b>XML:</b>
    //! \code
    //! <position type="vector2d" value="" />
    //! \endcode
    //!
    //! @param position      New position.
    void setPosition(const vector2di &position);
    //! Sets the source rectangle in the image
    //!
    //! <b>XML:</b>
    //! \code
    //! <sourceRect type="rect" value="" />
    //! \endcode
    //!
    //! @param rectangle      New source rectangle from where the image is retrieved.
    void setSourceRect(const rect<s32> &rectangle);
    //! Loads a texture from the given file and sets it as the texture to be drawn.
    //!
    //! <b>XML:</b>
    //! \code
    //! <texture type="string" value="" />
    //! \endcode
    //!
    //! @param fileName      Filename of the texture to load.
    void setTexture(const std::string &fileName);
    //! Sets the texture which should be drawn.
    //! @param texture       Pointer to the new texture.
    //! @note Not available in AngelScript.
    void setTexture(ITexture *texture);
    //! Sets whether the alpha color should be used.
    //!
    //! <b>XML:</b>
    //! \code
    //! <useAlphaColor type="bool" value="" />
    //! \endcode
    //!
    //! @param value         Value to enable or disble this option.
    void setUseAlphaColor(bool value);

    // Event functions
    //! Updates the component if the parent is rendered.
    //! @note For internal use only!
    void onRender(void *p);
    //! Pauses the component if the parent is paused.
    //! @note For internal use only!
    void onPause(void *p);
    //! Unpauses the component if the parent is unpaused.
    //! @note For internal use only!
    void onUnPause(void *p);
    //! Responds to changes of the texture attached to this component.
    //! @note For internal use only!
    void onTexture(void *p);

    // Serialization
    //! Serializes this object.
    //! @param attributes
    virtual bool onSerialization(SerializedAttributes *attributes);
    //! Deserializes this object.
    //! @param attributes
    virtual bool onDeserialization(SerializedAttributes *attributes);

private:

    // Private methods.
    //! Common initialisation method.
    void init();

    // Private members
    ITexture *mTexture;

    vector2di mPosition;
    rect<s32> mSourceRect;
    rect<s32> mClipRect;
    SColor mColor;
    SColor mAlphaColor;

    bool mVisible;
    bool mUseAlphaColor;
    bool mWasVisible;

    std::string mTextureFileName;
};

#endif
